package common.service.base.common;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import common.service.base.handler.ThreadLocalHandler;
import common.service.contants.MobileConfig;
import common.service.util.AesUtil;
import common.service.util.RedisUtil;
import common.service.util.TokenUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName MobileSession
 * @Description 分布式会话
 * @Author lixin
 * @Date 2020/02/10 下午3:41
 * Version 0.0.1
 **/
@Service
@Slf4j
public class MobileSession {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private SessionIdGenerator sessionIdGenerator;

    /**
     * 创建分布式会话
     */
    public String createSession(Long userId){
        return createSession(userId, MobileConfig.SESSION_TIMEOUT_SECONDS);
    }

    public String createSession(Long userId, int timeoutSecends){
        log.info("createSession userId:{},timeoutSecends:{}",userId,timeoutSecends);
        String sessionId = sessionIdGenerator.generateSessionId();
        log.info("createSession userId,sessionId:"+userId+"--"+sessionId);//122
       // Map<String,Object> map = new HashMap<String,Object>();
       // redisUtil.hmset(sessionId,map);
        log.info("createSession hmset key:{}",sessionId);
        ThreadLocalHandler.setThreadLocal(MobileConfig.SESSION_NAME,sessionId);
        String encrypt = AesUtil.encrypt(userId + TokenUtil.get2Randomint());
        return sessionId+encrypt;
    }
    
    /**
     * 销毁会话
     */
    public void destorySession(){
        String sessionId = (String) ThreadLocalHandler.getThreadLocal(MobileConfig.SESSION_NAME);
        if(sessionId.length()>96){
            sessionId =sessionId.substring(0,96);
        }
        redisUtil.hdel(sessionId,MobileConfig.SESSION_USR_INFO);
    }
    /**
     * 设置值
     */
    public void setAttribute(String key,Object value){
    	this.setAttribute(key,value,0);
    }
    /**
               * 设置值
    */
   public void setAttribute(String key,Object value, int timeoutSecends){
       String sessionId = (String) ThreadLocalHandler.getThreadLocal(MobileConfig.SESSION_NAME);
       Boolean flag = redisUtil.hset(sessionId,key,value);
       if(flag && timeoutSecends>0){
           redisUtil.expire(sessionId,timeoutSecends);
       }
       //刷新session时间
       log.info("createSession setAttribute key:{},expire:{}",sessionId,redisUtil.getExpire(sessionId));
   }
    /**
     * 获取值
     */
    public Object getAttribute(String key){
       return getAttribute(key, MobileConfig.SESSION_TIMEOUT_SECONDS);
    }
    
    public Object getAttribute(String key, int timeoutSecends){
        String sessionId = (String) ThreadLocalHandler.getThreadLocal(MobileConfig.SESSION_NAME);
        log.info("createSession 获取sessionid  sessionId:{},timeoutSecends:{}",sessionId,timeoutSecends);
        if(StringUtils.isNotBlank(sessionId)){
            if(sessionId.length()>96){
                sessionId =sessionId.substring(0,96);
            }
            Object hget = redisUtil.hget(sessionId, key);
            if(hget!=null){
                redisUtil.expire(sessionId,timeoutSecends);
            }
            //刷新session时间
            log.info("createSession getAttribute key:{},expire:{}",sessionId,redisUtil.getExpire(sessionId));
            return hget;
        }else {
            return null;
        }

    }

    /**
     * 获取值
     */
    public Object getAttribute(String key,String sessionId){
        if(StringUtils.isEmpty(sessionId)){
            sessionId = (String) ThreadLocalHandler.getThreadLocal(MobileConfig.SESSION_NAME);
        }
        if(sessionId.length()>96){
            sessionId =sessionId.substring(0,96);
        }
        return redisUtil.hget(sessionId, key);
    }


}
